//
//  LGPatientDetailHeader.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/5/20.
//  患者详情-患者信息

#import "LGPatientDetailHeader.h"

@interface LGPatientDetailHeader()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIImageView *headPic;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)UILabel *genderLabel;
/**   */
@property (nonatomic , strong)UILabel *ageLabel;
/**   */
@property (nonatomic , strong)UILabel *heightLabel;
/**   */
@property (nonatomic , strong)UILabel *weightLabel;
/**   */
@property (nonatomic , strong)UIButton *markBtn;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UILabel *keshiLabel;
/**   */
@property (nonatomic , strong)UILabel *doctorLabel;

/**   */
@property (nonatomic , strong)UILabel *roomTitle;
@property (nonatomic , strong)UILabel *roomLabel;

/**   */
@property (nonatomic , strong)UILabel *bedTitle;
@property (nonatomic , strong)UILabel *bedLabel;

@end

@implementation LGPatientDetailHeader

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.nameLabel.text = LGNSString(dataDic[@"name"]);
    self.numberLabel.text = [NSString stringWithFormat:@"  病案号：%@  ",dataDic[@"patientCode"]];
    NSString *sex = [LGNSString(dataDic[@"sex"]) isEqualToString:@"1"]?@"男":@"女";
    NSString *age = ([dataDic[@"age"] integerValue]>0)?[NSString stringWithFormat:@"%@岁",dataDic[@"age"]] : @"";
    NSString *height = ([dataDic[@"height"] integerValue]>0)?[NSString stringWithFormat:@"%@cm",dataDic[@"height"]] : @"";
    NSString *weight = ([dataDic[@"weight"] integerValue]>0)?[NSString stringWithFormat:@"%@kg",dataDic[@"weight"]] : @"";
    CGFloat ageWidth = (age.length>0)?viewPix(60):0;
    CGFloat heightWidth = (height.length>0)?viewPix(60):0;
    CGFloat weightWidth = (weight.length>0)?viewPix(60):0;
    self.genderLabel.text = sex;
    self.ageLabel.text = age;
    self.heightLabel.text = height;
    self.weightLabel.text  = weight;
    self.ageLabel.hidden = (age.length>0)?NO:YES;
    self.heightLabel.hidden = (height.length>0)?NO:YES;
    self.weightLabel.hidden = (weight.length>0)?NO:YES;
    [self.ageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(ageWidth));
    }];
    [self.heightLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(heightWidth));
    }];
    [self.weightLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(weightWidth));
    }];
    
    NSString *headPic = [sex isEqualToString:@"男"]?@"患者头像-男":@"患者头像-女";
    self.headPic.image = [UIImage imageNamed:headPic];
    
    NSString *ifTuber = LGNSString(dataDic[@"ifTuber"]);
    self.cancelBtn.hidden = [ifTuber isEqualToString:@"1"]?NO:YES;
    self.markBtn.selected = [ifTuber isEqualToString:@"1"]?YES:NO;
    self.markBtn.userInteractionEnabled = [ifTuber isEqualToString:@"1"]?NO:YES;
    CGFloat markW = [ifTuber isEqualToString:@"1"]?viewPix(94):viewPix(132);
    [self.markBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(markW));
    }];
    self.doctorLabel.text = LGNSString(dataDic[@"doctorName"]);
    self.keshiLabel.text = LGNSString(dataDic[@"categoryName"]);
    self.roomLabel.text = LGNSString(dataDic[@"deptName"]);
    self.bedLabel.text = LGNSString(dataDic[@"bedCode"]);
}

-(void)setType:(NSString *)type{
    _type = type;
    BOOL inSide = [type isEqualToString:@"inside"]?YES:NO;
    self.roomTitle.hidden = !inSide;
    self.roomLabel.hidden = !inSide;
    self.bedTitle.hidden = !inSide;
    self.bedLabel.hidden = !inSide;
}

//ifTuber：0取消标记，1标记患者
-(void)updateTuberStatus:(UIButton *)sender{
    NSString *ifTuber = (sender==self.markBtn)?@"1":@"0";
    if (self.delegate && [self.delegate respondsToSelector:@selector(updateTuberStatus:)]) {
        [self.delegate updateTuberStatus:ifTuber];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    UIView *baseView = [[UIView alloc] init];
    baseView.backgroundColor = [UIColor whiteColor];
    baseView.cornerRidus = 20;
    self.baseView = baseView;
    [self addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.equalTo(self).offset(viewPix(16));
        make.right.equalTo(self).offset(-viewPix(16));
    }];

    self.headPic = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(18), viewPix(54), viewPix(54))];
    self.headPic.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
    self.headPic.cornerRidus = viewPix(27);
    [baseView addSubview:self.headPic];
    
    self.nameLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headPic).offset(viewPix(2));
        make.left.equalTo(self.headPic.mas_right).offset(viewPix(14));
        make.height.equalTo(@(25));
    }];
    
    self.numberLabel = [UILabel lableWithText:@"  病案号：xxxxx  " colorString:@"#61616D" textFont:LGFont(13) textAlignment:NSTextAlignmentCenter lines:1];
    [self.numberLabel addBorder:@"#C5C4C4" lineWidth:1 cornerRidus:viewPix(11)];
    [baseView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(8));
        make.centerY.equalTo(self.nameLabel);
        make.height.equalTo(@(viewPix(22)));
    }];
    
    self.genderLabel = [self createBasicLabel];
    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(viewPix(6));
        make.left.equalTo(self.nameLabel).offset(-viewPix(20));
        make.width.equalTo(@(viewPix(60)));
        make.height.equalTo(@(viewPix(20)));
    }];
    
    self.ageLabel = [self createBasicLabel];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.equalTo(self.genderLabel);
        make.left.equalTo(self.genderLabel.mas_right);
        make.width.equalTo(@(viewPix(60)));
    }];
    
    self.heightLabel = [self createBasicLabel];
    [self.heightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.equalTo(self.genderLabel);
        make.left.equalTo(self.ageLabel.mas_right);
        make.width.equalTo(@(viewPix(70)));
    }];
    
    self.weightLabel = [UILabel lableWithText:@"" colorString:@"#61616D" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
    [self.baseView addSubview:self.weightLabel];
    [self.weightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.equalTo(self.genderLabel);
        make.left.equalTo(self.heightLabel.mas_right);
        make.width.equalTo(@(viewPix(60)));
    }];
   
    self.markBtn = [UIButton buttonWithTitle:@" 标记为结节患者" titleFont:LGFont(14) textColor:kThemeColor imageName:@"肺结节-未标记"];
    [self.markBtn setImage:[UIImage imageNamed:@"肺结节-已标记"] forState:UIControlStateSelected];
    [self.markBtn setSelectTitle:@" 结节患者" textColor:@"#FFFFFF"];
    [self.markBtn setBackgroundColor:@"#FFFFFF" selectColor:kThemeColor];
    [self.markBtn addBorder:kThemeColor lineWidth:1 cornerRidus:viewPix(14)];
    self.markBtn.clipsToBounds = YES;
    [self.markBtn addTarget:self action:@selector(updateTuberStatus:) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:self.markBtn];
    [self.markBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.genderLabel.mas_bottom).offset(viewPix(6));
        make.left.equalTo(self.nameLabel);
        make.width.equalTo(@(viewPix(132)));
        make.height.equalTo(@(viewPix(28)));
    }];
    
    self.cancelBtn = [UIButton buttonWithTitle:@"取消标记" titleFont:LGFont(14) normalColor:kThemeColor selectColor:@"#FFFFFF"];
    [self.cancelBtn addBorder:@"#E4E2FD" lineWidth:1 cornerRidus:viewPix(14)];
    [self.cancelBtn addTarget:self action:@selector(updateTuberStatus:) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.markBtn.mas_right).offset(viewPix(8));
        make.centerY.height.equalTo(self.markBtn);
        make.width.equalTo(@(viewPix(83)));
    }];
    
    UIView *bottomView = [self createBottomView];
    [baseView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.markBtn.mas_bottom).offset(viewPix(8));
        make.left.equalTo(baseView).offset(viewPix(12));
        make.right.equalTo(baseView).offset(-viewPix(12));
        make.bottom.equalTo(baseView).offset(-viewPix(8));
    }];
}

//性别、年龄、身高、体重
-(UILabel *)createBasicLabel{
    UILabel *label = [UILabel lableWithText:@"" colorString:@"#61616D" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
    label.clipsToBounds = YES;
    UIView *segmentView = [[UIView alloc]init];
    segmentView.backgroundColor = [UIColor colorWithString:@"#C5C4C4"];
    [label addSubview:segmentView];
    [segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(label).offset(-2);
        make.centerY.equalTo(label);
        make.width.equalTo(@(1));
        make.height.equalTo(@(15));
    }];
    [self.baseView addSubview:label];
    return label;
}


-(UIView *)createBottomView{
    UIView *baseView = [[UIView alloc]init];
    baseView.backgroundColor = [UIColor colorWithString:@"#FAFAFA"];
    baseView.cornerRidus = 10;
    
    UILabel *keshiTitle = [UILabel lableWithText:@"科室:" colorString:@"#968F8F" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    keshiTitle.frame = CGRectMake(viewPix(10), viewPix(10), viewPix(40), viewPix(20));
    [baseView addSubview:keshiTitle];
    
    self.keshiLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:self.keshiLabel];
    [self.keshiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(keshiTitle.mas_right);
        make.centerY.equalTo(keshiTitle);
    }];
    
    UILabel *doctorTitle = [UILabel lableWithText:@"所属医生:" colorString:@"#968F8F" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    doctorTitle.frame = CGRectMake((Screen_W-viewPix(56))/2.0+viewPix(10), kMinY(keshiTitle.frame), viewPix(65), viewPix(20));
    [baseView addSubview:doctorTitle];
    
    self.doctorLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:self.doctorLabel];
    [self.doctorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(doctorTitle.mas_right);
        make.centerY.equalTo(doctorTitle);
    }];
    
    self.roomTitle = [UILabel lableWithText:@"病房:" colorString:@"#968F8F" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    self.roomTitle.frame = CGRectMake(viewPix(10), viewPix(38), viewPix(40), viewPix(20));
    [baseView addSubview:self.roomTitle];
    
    self.roomLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:self.roomLabel];
    [self.roomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.roomTitle.mas_right);
        make.centerY.equalTo(self.roomTitle);
    }];
    
    self.bedTitle = [UILabel lableWithText:@"床号:" colorString:@"#968F8F" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    self.bedTitle.frame = CGRectMake((Screen_W-viewPix(56))/2.0+viewPix(10), viewPix(38), viewPix(40), viewPix(20));
    [baseView addSubview:self.bedTitle];
    
    self.bedLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:self.bedLabel];
    [self.bedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bedTitle.mas_right);
        make.centerY.equalTo(self.bedTitle);
    }];
    return baseView;
}

@end
